#include <string.h>
#include <stdio.h>

#include <windows.h>

#include "lib.h"
#include "rdlocalsql.h"
#include "rdgame.h"
#include "rdsaved.h"

#define MAXOUTCOMESTR	8				// max bytes in outcome
#define MAXGIDSTR		32				// max bytes in GameID
#define MAXCARDS		5				// max # cards in hand (stored in dbase)
#define MAXCARDSTR	((MAXCARDS<<1)+1)	// hex representation

RedDogSQL::RedDogSQL(char* DBHost, char* DBName, char* DBPassword)
{
}

void
RedDogSQL::outcomeToDB(uint8 Outcome, char* Dest)
{
	strcpy(Dest, "hello.");
}

void
RedDogSQL::DBtoRedDogGameID(const char* src, RedDogGameID dest)
{
	// ignore precision, scale, sign from DBDECIMAL since we know it is
	// always 12, 0, 1.
	//
	memcpy(dest, src + 3, 8);	// src is DBDECIMAL* (+3 skips as above)
}

void
RedDogSQL::RedDogGameIDtoDB(RedDogGameID src, char* dest)
{
#if 0
	DBDECIMAL id;

	memset(&id, 0, sizeof(DBDECIMAL));
	id.precision = 12;
	id.scale = 0;
	id.sign = 1;
	memcpy(id.val, src, 8);
#endif
	sprintf(dest, "stuff");
//	dbconvert(dbconn, SQLDECIMAL, (LPBYTE)&id, sizeof(DBDECIMAL),
//		SQLCHAR, (LPBYTE)dest, -1);
}

bool8
RedDogSQL::createGame(PID pid, bool8 forMoney, RedDogGameID gid,
	int32 bet, int32* balance)
{
#if 0
	char betstr[MAXMONEYSTR];

	dollarsToDB(bet, betstr);
	dbcmd(dbconn, "exec pub_CreateGameRedDog ");
	dbfcmd(dbconn, "'%-8.8s', %d, %s", pid, (int32)(forMoney != 0), betstr);

	if (SUCCEED == dbsqlexec(dbconn))
	{
		// stored proc results
		//
		if (SUCCEED == dbresults(dbconn))
		{
			while (dbnextrow(dbconn) != NO_MORE_ROWS)
			{
				DBtoRedDogGameID((char*)dbdata(dbconn, 1), gid);
				DBtoDollars((DBMONEY*)dbdata(dbconn, 2), balance);
			}

			if (dbcount(dbconn) == 1)
			{
				error = NoError;
				return true;
			}
		}
	}

	error = ExecFailed;
#endif
	return false;
}

bool8
RedDogSQL::deal(PID pid, RedDogGameID gid, int32 numPlayer, uint8* player)
{
#if 0
	char gidstr[MAXGIDSTR];
	char cardstr[MAXCARDSTR];

	if (!dbconn)
		return false;

	RedDogGameIDtoDB(gid, gidstr);

	dbcmd(dbconn, "exec pub_RedDogDeal ");
	dbfcmd(dbconn, "'%-8.8s', %s", pid, gidstr);

	cardsToDB(numPlayer, player, cardstr);
	dbcmd(dbconn, ", 0x");
	dbcmd(dbconn, cardstr);

	if (SUCCEED == dbsqlexec(dbconn))
	{
		// stored proc results
		//
	if (SUCCEED == dbresults(dbconn))
		{
			while (dbnextrow(dbconn) != NO_MORE_ROWS)
				/* ignore results */ ;

			error = NoError;
			return true;
		}
	}

	error = ExecFailed;
#endif
	return false;
}

bool8
RedDogSQL::gameDone(PID pid, bool8 forMoney, RedDogGameID gid, 
	int32 outcome, int32 balanceAdjust, int32* balance)
{
#if 0
	char gidstr[MAXGIDSTR];
	char moneystr[MAXMONEYSTR];
	char outcomestr[MAXOUTCOMESTR];

	if (!dbconn)
		return false;

	RedDogGameIDtoDB(gid, gidstr);
	outcomeToDB((uint8)(outcome & 0xffL), outcomestr);

	dbcmd(dbconn, "exec pub_RedDogGameDone ");
	dbfcmd(dbconn, "'%-8.8s', %d, %s",
		pid, (int32)(forMoney != 0), gidstr);

	dbcmd(dbconn, ", 0x");
	dbcmd(dbconn, outcomestr);

	dollarsToDB(balanceAdjust, moneystr);
	dbfcmd(dbconn, ", %s", moneystr);

	if (SUCCEED == dbsqlexec(dbconn))
	{
		// stored proc results
		//
		if (SUCCEED == dbresults(dbconn))
		{
			while (dbnextrow(dbconn) != NO_MORE_ROWS)
			{
				DBtoDollars((DBMONEY*)dbdata(dbconn, 1), balance);
			}

			if (dbcount(dbconn) == 1)
			{
				error = NoError;
				return true;
			}
		}
	}

	error = ExecFailed;
#endif
	return false;
}

bool8
RedDogSQL::hasSavedGame(PID pid, bool8 forMoney, RedDogGameID gid,
	RedDogSavedGame* dest)
{
#if 0
	if (!dbconn)
		return false;

	dbcmd(dbconn, "exec pub_RedDogGetSavedGame ");
	dbfcmd(dbconn, "'%-8.8s', %d", pid, (int32)(forMoney != 0));

	if (SUCCEED == dbsqlexec(dbconn))
	{
		// stored proc results
		//
		if (SUCCEED == dbresults(dbconn))
		{
			bool8 result = false;

			while (dbnextrow(dbconn) != NO_MORE_ROWS)
			{
				if (*(DBINT*)dbdata(dbconn, 1) != 0)
				{
					result = true;
					int32 numPlayerCards = 2;

					DBtoRedDogGameID((char*)dbdata(dbconn, 2), gid);
					DBtoDollars((DBMONEY*)dbdata(dbconn, 3), &dest->bet);
					DBtoCards((char*)dbdata(dbconn, 5), dbdatlen(dbconn, 5),
						&numPlayerCards, dest->playerCards, MAXCARDS);
				}
			}

			if (dbcount(dbconn) == 1)
			{
				error = NoError;
				return result;
			}
		}
	}

	error = ExecFailed;
#endif
	return true;
}

bool8
RedDogSQL::removeSavedGame(PID pid, bool8 forMoney)
{
#if 0
	if (!dbconn)
		return false;

	dbcmd(dbconn, "exec pub_RedDogRemoveSavedGame ");
	dbfcmd(dbconn, "'%-8.8s', %d", pid, (int32)(forMoney != 0));

	if (SUCCEED == dbsqlexec(dbconn))
	{
		// stored proc results
		//
		if (SUCCEED == dbresults(dbconn))
		{
			while (dbnextrow(dbconn) != NO_MORE_ROWS)
				/* ignore results */ ;

			error = NoError;
			return true;
		}
	}

	error = ExecFailed;
#endif
	return false;
}

bool8
RedDogSQL::saveGame(PID pid, bool8 forMoney, RedDogGameID gid,
	RedDogSavedGame* src)
{
#if 0
	char gidstr[MAXGIDSTR];

	if (!dbconn)
		return false;

	RedDogGameIDtoDB(gid, gidstr);

	dbcmd(dbconn, "exec pub_RedDogSaveGame ");
	dbfcmd(dbconn, "'%-8.8s', %d, %s", pid, (int32)(forMoney != 0), gidstr);

	if (SUCCEED == dbsqlexec(dbconn))
	{
		// stored proc results
		//
		if (SUCCEED == dbresults(dbconn))
		{
			while (dbnextrow(dbconn) != NO_MORE_ROWS)
				/* ignore results */ ;

			error = NoError;
			return true;
		}
	}

	error = ExecFailed;
#endif
	return false;
}
